﻿module Octopus.OctopusService.General

//------- Files stuff

let internal getServiceBinPath =
  let s = System.Reflection.Assembly.GetEntryAssembly().Location
  (new System.IO.DirectoryInfo(s)).Parent

let internal buildPath (parent:System.IO.DirectoryInfo) subPath =
  let s = parent.FullName + "\\" + subPath
  new System.IO.DirectoryInfo(s)

let internal buildFilePath (path:System.IO.DirectoryInfo) filename =
  let s = path.FullName + "\\" + filename
  new System.IO.FileInfo(s)

let internal getPath parent subPath =
  let d = buildPath parent subPath
  if d.Exists then d
  else failwith "Directory not found"

let internal getFile path filename =
  let f = buildFilePath path filename
  if f.Exists then f
  else failwith "File not found"

//------- Files stuff

let parseMultipleHostname hostnames =
  if hostnames = null || hostnames = "" then failwith "Invalid parameter"
  if hostnames.Contains(" ") then failwith "Invalid parameter"

  let numOfDollars = [| for x in hostnames.ToCharArray() do if x = '$' then yield 1 |].Length
  if numOfDollars = 0 then
    [| hostnames |]
  else if numOfDollars = 1 || numOfDollars > 2 then
    failwith "Invalid parameter"
  else
    let indexFst = hostnames.IndexOf('$')
    let indexSnd = hostnames.LastIndexOf('$')
    let firstName = hostnames.Substring(0, indexFst)
    let lastName = hostnames.Substring(indexSnd + 1, hostnames.Length - indexSnd - 1)
    let middlePart = hostnames.Substring(indexFst + 1, indexSnd - indexFst - 1)

    if middlePart = "" || not(middlePart.Contains("-")) then failwith "Invalid parameter"

    let splitted = middlePart.Split('-')
    let fstNumber = System.Int32.Parse(splitted.[0])
    let sndNubmer = System.Int32.Parse(splitted.[1])

    [| for i = fstNumber to sndNubmer do yield sprintf "%s%d%s" firstName i lastName |]
